//
//  ZZProgressVIew.m
//  QQplayground
//
//  Created by zhangxy on 2018/7/4.
//  Copyright © 2018年 zhang. All rights reserved.
//

#import "ZZProgressVIew.h"
#import "UIImage+ImageWithColor.h"

@interface ZZProgressVIew(){
    
}
@property(nonatomic,strong) UIColor *bgColor;
@property(nonatomic,strong) UIColor *tintColor;

@property(nonatomic,strong) UILabel *labValue;
@property(nonatomic,strong) UIImageView *imgView;
@property(nonatomic,strong) UIButton *btnTitle;

@end

@implementation ZZProgressVIew

-(id)initWithBgColor:(UIColor *) bgColor tintColor:(UIColor *) tintColor frame:(CGRect ) f value:(NSDictionary *) value{
    self = [super initWithFrame:f];
    if(self){
        _bgColor = bgColor;
        _tintColor = tintColor;
        
        self.backgroundColor = bgColor;
        self.layer.cornerRadius = 3.0f;
        self.layer.masksToBounds = YES;
        
        _imgView = [[UIImageView  alloc] initWithFrame:CGRectMake(0, 0, f.size.width * [value[@"value"] floatValue], f.size.height)];
        [_imgView setBackgroundColor:tintColor];
        _imgView.layer.cornerRadius = 3.0f;
        _imgView.layer.masksToBounds = YES;
        [self addSubview:_imgView];
        
        _labValue = [[UILabel alloc] initWithFrame:_imgView.bounds];
        [_labValue setTextAlignment:NSTextAlignmentRight];
        [_labValue setFont:FontThirteen];
        [_labValue setTextColor:UIColorFromRGB(TextWhiteColor)];
        [_labValue setText:[NSString stringWithFormat:@"%d%%",(int)([value[@"value"] floatValue]*100)]];
//        [self addSubview:_labValue];
        
        
        _btnTitle = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btnTitle setTitle:value[@"title"] forState:0];
//        [_btnTitle setImage:[UIImage imageNamed:value[@"icon"]] forState:0];
        [_btnTitle.titleLabel setFont:FontThirteen];
        [_btnTitle setTitleColor:UIColorFromRGB(TextWhiteColor) forState:0];
        [_btnTitle setImageEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 5)];
        [_btnTitle setFrame:self.bounds];
        [_btnTitle setTitleEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
        [_btnTitle setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
        [_btnTitle setBackgroundColor:UIColor.clearColor];
        [_btnTitle setBackgroundImage:[UIImage imageWithColor:tintColor] forState:UIControlStateHighlighted];
        [_btnTitle addTarget:self action:@selector(btnClickListener:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_btnTitle];
    }
    return self;
}


-(void)changeValue:(NSDictionary *)value{
    [_labValue setFrame:_imgView.bounds];
    [_labValue setText:[NSString stringWithFormat:@"%d%%",(int)([value[@"value"] floatValue]*100)]];
    [_btnTitle setTitle:value[@"title"] forState:0];
//    [_btnTitle setImage:[UIImage imageNamed:value[@"icon"]] forState:0];
    
    [UIView animateWithDuration:0.25 animations:^{
        [_imgView setFrame:CGRectMake(0, 0, self.frame.size.width * [value[@"value"] floatValue], self.frame.size.height)];
    }];
}


-(void)btnClickListener:(UIButton *)btn{
    DLog(@"点击了%zd",self.tag);

    if(_OnItemClickBlock){
        _OnItemClickBlock(self.tag,@"");
    }
    
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
